//
//  XDShareView.m
//  OAMobile
//
//  Created by wangweichao on 2019/2/13.
//  Copyright © 2019年 李天露. All rights reserved.
#import "XDShareView.h"
#import "Masonry.h"
#import <ShareSDK/ShareSDK.h>
#import <MessageUI/MessageUI.h>
typedef void (^shareEvent)(void);
@interface IconView : UIView
@property(nonatomic,copy)shareEvent block;
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)UIButton *iconImage;
@end


@implementation IconView
-(instancetype)initWithBlock:(shareEvent)ablock{
    if(self = [super init]){
        self.block = ablock;
        [self addSubview:self.iconImage];
        [self addSubview:self.titleLab];
        
        [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.and.height.equalTo(@(SCREEN_WIDTH/4 - 25));
            make.top.offset(0);
            make.left.offset(12.5);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.iconImage.mas_bottom).with.offset(5);
            make.centerX.mas_equalTo(self.iconImage);
            make.height.equalTo(@20);
        }];
        [self.iconImage addTarget:self action:@selector(tapEvent) forControlEvents:UIControlEventTouchDown];
    }
    return self;
}
-(void)tapEvent{
    self.block();
}
-(UILabel *)titleLab{
    if(!_titleLab){
        _titleLab = [UILabel new];
        _titleLab.font = [UIFont systemFontOfSize:12];
        _titleLab.textColor = [UIColor grayColor];
    }
    return _titleLab;
}
-(UIButton *)iconImage{
    if(!_iconImage){
        _iconImage = [UIButton new];
    }
    return _iconImage;
}
@end


@interface XDShareView()

@property(nonatomic,strong)UIView *backGround;
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)IconView *wxFriend;
@property(nonatomic,strong)IconView *wxMoments;
@property(nonatomic,strong)IconView *sina;
@property(nonatomic,strong)IconView *sms;
@property(nonatomic,strong)IconView *qq;
@property(nonatomic,strong)IconView *qqZone;
@property(nonatomic,strong)UIButton *cancelBtn;
@end

@implementation XDShareView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)]){
        [self layoutAllSubviews];
    }
    return self;
}

-(void)shareViewShow{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.backgroundColor = [UIColor colorWithRed:.0f/255 green:.0f/255 blue:.0f/255 alpha:0.4];
        }];
    }];
    
}
-(void)shareViewhidden{
    [UIView animateWithDuration:0.2 animations:^{
        self.backgroundColor = [UIColor clearColor];
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
}

-(void)layoutAllSubviews{
    [self addSubview:self.backGround];
    [self.backGround mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.bottom.offset(0);
        make.top.offset(SCREEN_HEIGHT - 220);
    }];
    
    [self.backGround addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(15);
        make.left.offset(15);
    }];
    
    [self.backGround addSubview:self.wxFriend];
    [self.wxFriend mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLab.mas_bottom).with.offset(15);
        make.width.and.height.equalTo(@(SCREEN_WIDTH/4));
        make.left.offset(0);
    }];
    
    [self.backGround addSubview:self.wxMoments];
    [self.wxMoments mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLab.mas_bottom).with.offset(15);
        make.width.and.height.equalTo(@(SCREEN_WIDTH/4));
        make.left.offset(SCREEN_WIDTH/4);
    }];
    
    [self.backGround addSubview:self.qq];
    [self.qq mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLab.mas_bottom).with.offset(15);
        make.width.and.height.equalTo(@(SCREEN_WIDTH/4));
        make.left.offset(SCREEN_WIDTH/4*2);
    }];
    
    [self.backGround addSubview:self.qqZone];
    [self.qqZone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLab.mas_bottom).with.offset(15);
        make.width.and.height.equalTo(@(SCREEN_WIDTH/4));
        make.left.offset(SCREEN_WIDTH/4*3);
    }];
    
    [self.backGround addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.bottom.with.offset(0);
        make.height.equalTo(@50);
    }];
    
}

#pragma mark -- lazy alloc
-(UIView *)backGround{
    if(!_backGround){
        _backGround = [UIView new];
        _backGround.backgroundColor = [UIColor colorWithRed:223.0f/255 green:223.0f/255 blue:225.0f/255 alpha:1];
    }
    return _backGround;
}
-(UILabel *)titleLab{
    if(!_titleLab){
        _titleLab = [UILabel new];
        _titleLab.text = @"分享到";
        _titleLab.textColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:61.0f/255 alpha:1];
        _titleLab.font = [UIFont systemFontOfSize:16];
    }
    return _titleLab;
}
-(IconView *)wxFriend{
    
    if(!_wxFriend){
        _wxFriend = [[IconView alloc] initWithBlock:^{
            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            NSString* shareType = [self.shareData objectForKey:@"shareType"];
            if(shareType == nil || [shareType isEqualToString:@"1"]){
            [shareParams SSDKSetupWeChatParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"wechatUrl"] thumbImage:nil image:[self.shareData objectForKey:@"imgUrl"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeWechatSession];
            } else{
                 [shareParams SSDKSetupWeChatParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"wechatUrl"] thumbImage:nil image:[self.shareData objectForKey:@"imgUrl"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeWechatSession];
            }
            //
            [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                
            }];
        }];
        [_wxFriend.iconImage setImage:[UIImage imageNamed:@"wxficon"] forState:UIControlStateNormal];
        _wxFriend.titleLab.text = @"微信好友";
    }
    return _wxFriend;
}
-(IconView *)wxMoments{
    if(!_wxMoments){
        _wxMoments = [[IconView alloc] initWithBlock:^{
            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            NSString* shareType = [self.shareData objectForKey:@"shareType"];
            if(shareType == nil || [shareType isEqualToString:@"1"]){
            [shareParams SSDKSetupWeChatParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"wechatUrl"] thumbImage:nil image:[self.shareData objectForKey:@"imgUrl"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
            }else{
                [shareParams SSDKSetupWeChatParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"wechatUrl"] thumbImage:nil image:[self.shareData objectForKey:@"imgUrl"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
            }
            [ShareSDK share:SSDKPlatformSubTypeWechatTimeline parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                
            }];
        }];
        [_wxMoments.iconImage setImage:[UIImage imageNamed:@"wxmicon"] forState:UIControlStateNormal];
        _wxMoments.titleLab.text = @"微信朋友圈";
    }
    return _wxMoments;
}
-(IconView *)sina{
    if(!_sina){
        _sina = [[IconView alloc] initWithBlock:^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sinaweibo://"]];
        }];
        [_sina.iconImage setImage:[UIImage imageNamed:@"sinaicon"] forState:UIControlStateNormal];
        _sina.titleLab.text = @"新浪微博";
    }
    return _sina;
}
-(IconView *)sms{
    if(!_sms){
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"text":@"wangweichao"}];
        _sms = [[IconView alloc] initWithBlock:^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageUI" object:dic];
        }];
        [_sms.iconImage setImage:[UIImage imageNamed:@"smsicon"] forState:UIControlStateNormal];
        _sms.titleLab.text = @"短信";
    }
    return _sms;
}
-(IconView *)qq{
    if(!_qq){
        
        
        //         NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"text":@"https://uat.healthstarknx.com//hestar-vue/product/110032"}];
        _qq = [[IconView alloc] initWithBlock:^{
            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            NSString* shareType = [self.shareData objectForKey:@"shareType"];
            if(shareType == nil || [shareType isEqualToString:@"1"]){
            [shareParams SSDKSetupQQParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"link"] audioFlashURL:nil videoFlashURL:nil thumbImage:[self.shareData objectForKey:@"imgUrl"] images:[self.shareData objectForKey:@"imgUrl"] type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQQFriend];
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                [dic setObject:[self.shareData objectForKey:@"link"]  forKey:@"text"];
                [ShareSDK share:SSDKPlatformSubTypeQQFriend parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                    
                }];
            }else{
                 [shareParams SSDKSetupQQParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:nil thumbImage:nil  image:[self.shareData objectForKey:@"imgUrl"]   type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeQQFriend];
                [ShareSDK share:SSDKPlatformSubTypeQQFriend parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                       NSLog(@"调用失败%@",error);
                }];
//            
             
            }
           
        }];
        [_qq.iconImage setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal];
        _qq.titleLab.text = @"QQ";
    }
    return _qq;
}
-(IconView *)qqZone{
    if(!_qqZone){
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"text":@"wangweichao"}];
        _qqZone = [[IconView alloc] initWithBlock:^{
            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            NSString* shareType = [self.shareData objectForKey:@"shareType"];
            if(shareType == nil || [shareType isEqualToString:@"1"]){
            [shareParams SSDKSetupQQParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"link"] audioFlashURL:nil videoFlashURL:nil thumbImage:[self.shareData objectForKey:@"imgUrl"] images:[self.shareData objectForKey:@"imgUrl"] type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQZone];
            }else{
                [shareParams SSDKSetupQQParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"link"] thumbImage:[self.shareData objectForKey:@"imgUrl"]  image:[self.shareData objectForKey:@"imgUrl"] type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeQZone];
//                 [shareParams SSDKSetupQQParamsByText:[self.shareData objectForKey:@"desc"] title:[self.shareData objectForKey:@"title"] url:[self.shareData objectForKey:@"link"] audioFlashURL:nil videoFlashURL:nil thumbImage:[self.shareData objectForKey:@"imgUrl"] images:[self.shareData objectForKey:@"imgUrl"] type:SSDKContentTypeImage forPlatformSubType:SSDKPlatformSubTypeQZone];
            }
            [ShareSDK share:SSDKPlatformSubTypeQZone parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                
            }];
        }];
        [_qqZone.iconImage setImage:[UIImage imageNamed:@"qzone"] forState:UIControlStateNormal];
        _qqZone.titleLab.text = @"QQ空间";
    }
    return _qqZone;
}
-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton new];
        _cancelBtn.backgroundColor = [UIColor whiteColor];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(shareViewhidden) forControlEvents:UIControlEventTouchDown];
    }
    return _cancelBtn;
}
@end
